import $  from 'jquery';
import config from '../config/config';
import store  from  '../store';
import {Message}    from 'element-ui';
import router from "../router";
  
 // 默认登录的人 去登录【含过期】
 const to_login=()=>{
      localStorage.removeItem('store');
       router.push({ path: '/login' });
 }

  const $api= {
  /**
   * 发送同步请求W
   * @param arrays
   */
    sendSync : function (...arrays) {
      //alert(arrays);
      console.log("arrays=",arrays);
        var ret = null;
        let post_data = {
          token : store.getters.getToken,
          data:  arrays[1] || {}
        }
        post_data = JSON.stringify(post_data);
        $.ajaxSetup({async : arrays[3] || false});
        $.ajax({
          type: arrays[2] ? arrays[2].toUpperCase() : 'POST',
          url: config.serverURL + arrays[0],
          data: post_data || {},
          dataType: 'json',
          //去除header 不然移动云会报错！！！!
          success: res => {
            console.log(res);
            ret = res;
          }
        });
       $.ajaxSetup({async : true});
        console.log();

        //判断中间件是否正常通过
        if(parseInt(ret.code) == 400){
            Message.error(ret.msg);
            to_login();
        }
        if(ret.code == 'token_missing'){
            // Message.error(ret.msg);
            to_login();
        }

        return ret;
      },
      upload:function(...arrays) {
        return new Promise(
          (resolve, reject) => {
            $.ajax({
              type: 'POST',
              url: config.serverURL + arrays[0],
              data:arrays[1],
              dataType: 'json',
              contentType:false,
              processData:false,
          //去除header 不然移动云会报错！！！!
              success: res => {
                resolve(res)
              },
              error: err => {
                reject(err)
              }
            });
          }
        )
      },

      //跳转外部url地址，比如用于下载 excel 导出
      getUrl(url){
        $.ajax({
          type: 'GET',
          url: url
        })
      }
}


export default $api